function allCovs = GenerateCovariates_simple(saveBool, sequenceFiles)
%AdaptID_v1_0: Run the Individual Adaptation experiment
%           AdaptID_v1_0(protocolListFileName)
%
%   AdaptID_v1_0(protocolListFileName) initializes the Individual
%   Adaptation fMRI experiment. This experiment aims to investigate the
%   effect of stimulus history on the current evoked response, when stimuli
%   on serial position -1 to -N are manipulated in a counterbalanced way.
%
%   IMPORTANT!
%   It is recommended that this file is located on ./Experiment/AdaptID_v1_0.m
%   The program will create the ./Data directory for saving the data
%
%
%   Inputs
%   -------
%   [protocolListFileName]: path to a file that should contain a
%           description of the protocols to be used. If nothing is entered,
%           the file ./Config/ProtocolList.cfg will be used
%
%
%   Outputs
%   -------
%   ./Data/[expType]/[protocol]/[subjID]/[subjID]-[protocol]-[iteration].mat
%           this file will store all experimental info, experiment
%           parameters, stimuli used, and data collected
%
%   Examples
%   -------
%    i) AdaptID_v1_0('./protocols.txt')
%   ii) AdaptID_v1_0
%
%   _______________________________________________
%   by Marcelo G Mattar (07/24/2012)
%   mattar@sas.upenn.edu


%% CHECK INPUTS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if nargin < 2
    sequenceFiles = {...
        '/Users/marcelomattar/Dropbox/Marcelo/UPenn/Documents/Projects/AdaptID/Experiment/Config/seqs/3targetsA.txt' ...
        '/Users/marcelomattar/Dropbox/Marcelo/UPenn/Documents/Projects/AdaptID/Experiment/Config/seqs/3targetsB.txt' ...
        '/Users/marcelomattar/Dropbox/Marcelo/UPenn/Documents/Projects/AdaptID/Experiment/Config/seqs/4targetsA.txt' ...
        '/Users/marcelomattar/Dropbox/Marcelo/UPenn/Documents/Projects/AdaptID/Experiment/Config/seqs/4targetsB.txt' ...
        '/Users/marcelomattar/Dropbox/Marcelo/UPenn/Documents/Projects/AdaptID/Experiment/Config/seqs/5targetsA.txt' ...
        '/Users/marcelomattar/Dropbox/Marcelo/UPenn/Documents/Projects/AdaptID/Experiment/Config/seqs/5targetsB.txt' ...
        };
else
    if ~iscell(sequenceFiles)
        sequenceFiles = cellstr(sequenceFiles);
    end
end

if nargin < 1
    saveBool = 0;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%% PARAMETERS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% General
%expInfo.expType = 'fMRI'; % Experiment type. Enter "fMRI" or "Behavioal" to specify the directory where the data will be saved

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



numFiles = length(sequenceFiles);

% Initialize covariates
mainEffect = cell(length(sequenceFiles),1);
targetTrials = cell(length(sequenceFiles),1);
afterTargets = cell(length(sequenceFiles),1);
afterBlanks = cell(length(sequenceFiles),1);
carryOver1 = cell(length(sequenceFiles),1);


for seqnum = 1:numFiles
    
    % Load the sequence
    thisSeq = double(importdata(sequenceFiles{seqnum}));
    
    mainEffect{seqnum} = ~(thisSeq==0);
    targetTrials{seqnum} = (thisSeq==-1);
    
    % Determine the trials following a target trial
    afterTargets{seqnum} = zeros(length(thisSeq),1);
    afterTargets{seqnum}(find(thisSeq==-1)+1) = 1;
    
    % Determine the trials following a blank block
    afterBlanks{seqnum} = zeros(length(thisSeq),1);
    afterBlanks{seqnum}(findEndOfBlankBlock(thisSeq)+1) = 1;
    
    % Calculate the difference between each stimulus and the previous ones
    carryOver1{seqnum} = thisSeq - [thisSeq(end); thisSeq(1:(end-1))];
    % Remove any non-typical trial from the analysis
    carryOver1{seqnum} = carryOver1{seqnum} .* ~afterTargets{seqnum} .* ~afterBlanks{seqnum} .* ~targetTrials{seqnum} .* mainEffect{seqnum};
    % Group any non-zero transition as a unit transition
    carryOver1{seqnum} = ~carryOver1{seqnum}==0;
    
    if saveBool
        if ~exist('./covariates/', 'dir')
            mkdir('./covariates/');
        end
        i = max(strfind(sequenceFiles{seqnum}, '/'));
        j = max(strfind(sequenceFiles{seqnum}, '.'));
        sequenceName = sequenceFiles{seqnum}((i+1):(j-1));
        
        dlmwrite(['./covariates/' sequenceName '_mainEffect.txt'],mainEffect{seqnum});
        dlmwrite(['./covariates/' sequenceName '_targetTrials.txt'],targetTrials{seqnum});
        dlmwrite(['./covariates/' sequenceName '_afterTargets.txt'],afterTargets{seqnum});
        dlmwrite(['./covariates/' sequenceName '_afterBlanks.txt'],afterBlanks{seqnum});
        dlmwrite(['./covariates/' sequenceName '_carryOver1.txt'],carryOver1{seqnum});
    end
end

allCovs.mainEffect = mainEffect;
allCovs.targetTrials = targetTrials;
allCovs.afterTargets = afterTargets;
allCovs.afterBlanks = afterBlanks;
allCovs.carryOver1 = carryOver1;




function endOfBlankBlocks = findEndOfBlankBlock(sequence)

    startOfBlankBlocks = [];
    endOfBlankBlocks = [];
    
    if sequence(1) == 0; % If the first element is a blank
        startOfBlankBlocks = 1;
    end
    
    for i=1:(length(sequence)-1)
        if (sequence(i)~=0) && (sequence(i+1)==0)
            startOfBlankBlocks = [startOfBlankBlocks, (i+1)];
        end
        if (sequence(i)==0) && (sequence(i+1)~=0)
            endOfBlankBlocks = [endOfBlankBlocks, i];
        end
    end
